package com.yflutter.yfree_app.activity

import android.view.View
import android.view.ViewGroup
import com.flutter_yfree.YFlutterPlugin
import com.yflutter.yfree_app.MyApplication
import com.yflutter.yfree_app.util.IsHarmonyOsUtils
import com.yflutter.yfree_app.util.MyPlugin
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity : FlutterActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        MyApplication.mainFlutterActivity = this
        YFlutterPlugin.registerWith(this, flutterEngine)
        MyPlugin.register()
    }

    override fun onRestart() {
        super.onRestart()
        if (IsHarmonyOsUtils.isHarmonyOs()) {
            val decorView = window.decorView
            decorView.visibility = View.INVISIBLE
            decorView.visibility = View.VISIBLE
            decorView.invalidate()
            decorView.postInvalidate()
            decorView.requestLayout()
            val view = findViewById<View>(FLUTTER_VIEW_ID)
            view.visibility = View.INVISIBLE
            view.visibility = View.VISIBLE
            view.invalidate()
            view.postInvalidate()
            view.requestLayout()
            val group = view.parent as View
            group.visibility = View.INVISIBLE
            group.visibility = View.VISIBLE
            group.invalidate()
            group.postInvalidate()
            group.requestLayout()
            (group as ViewGroup).removeView(view)
            setContentView(view)
        }
    }
}
